
.PHONY: clean

### TARGET ###
PS=@PSIO@
DPDK=@DPDK@
NETMAP=@NETMAP@
LRO=@LRO@
MTCP_LIB_DIR=../lib
MTCP_LIB=libmtcp.a
MTCP_HDR_DIR=../include
MTCP_HDR = mtcp_api.h mtcp_epoll.h


### GCC ###
GCC=@CC@

### FLAGS ###
GCC_OPT = -m64 -Wall -fPIC -fgnu89-inline
#DBG_OPT = -DDBGMSG -DDBGFUNC -DSTREAM -DSTATE -DTSTAT -DAPP -DEPOLL
#DBG_OPT = -DDBGMSG -DDBGFUNC -DSTREAM -DSTATE
#DBG_OPT += -DPKTDUMP
#DBG_OPT += -DDUMP_STREAM
#GCC_OPT += -g -DNETSTAT -DINFO -DDBGERR -DDBGCERR
GCC_OPT += -DNDEBUG -O3 -DNETSTAT -DINFO -DDBGERR -DDBGCERR
#GCC_OPT += -DNDEBUG -g -DNETSTAT -DINFO -DDBGERR -DDBGCERR
GCC_OPT += $(DBG_OPT)

ifeq ($(LRO),1)
GCC_OPT += -DENABLELRO
endif


### LIBRARIES AND INCLUDES ###
INC_DIR=./include
INC= -I$(INC_DIR)

ifeq ($(PS),1)
LIBS = -lps
else
INC += -DDISABLE_PSIO
endif

ifeq ($(NETMAP),1)
# do nothing
else
INC += -DDISABLE_NETMAP
endif

# PacketShader LIBRARY and HEADER
PS_DIR=../../io_engine
INC += -I$(PS_DIR)/include

# DPDK LIBRARY and HEADER
DPDK_INC=@DPDKLIBPATH@/include
DPDK_LIB=@DPDKLIBPATH@/lib/
# CFLAGS for DPDK-related compilation
ifeq ($(DPDK), 1)
DPDK_MACHINE_FLAGS = $(shell cat @DPDKLIBPATH@/include/cflags.txt)
INC += ${DPDK_MACHINE_FLAGS} -I${DPDK_INC} -include $(DPDK_INC)/rte_config.h
else
INC += -DDISABLE_DPDK
endif

ifeq ($(wildcard /usr/lib/libhugetlbfs.so),) 
else
	GCC_OPT += -DHUGEPAGE
endif


### SOURCE CODE ###
SRCS = core.c tcp_stream.c config.c api.c eventpoll.c socket.c pipe.c \
	   tcp_util.c eth_in.c ip_in.c tcp_in.c eth_out.c ip_out.c tcp_out.c \
	   arp.c timer.c cpu.c rss.c addr_pool.c fhash.c memory_mgt.c logger.c debug.c \
	   tcp_rb_frag_queue.c tcp_ring_buffer.c tcp_send_buffer.c tcp_sb_queue.c tcp_stream_queue.c \
	   psio_module.c io_module.c dpdk_module.c netmap_module.c icmp.c

OBJS = $(patsubst %.c,%.o,$(SRCS))
DEPS = $(patsubst %.c,.%.d,$(SRCS))


### GOALS ###
all: default

default: $(OBJS) $(MTCP_HDR)
	mkdir -p $(MTCP_LIB_DIR)
	ar rvs $(MTCP_LIB_DIR)/$(MTCP_LIB) $(OBJS)

$(OBJS): %.o: %.c Makefile
	$(GCC) $(GCC_OPT) $(INC) -c $< -o $@
$(DEPS): .%.d: %.c Makefile
	$(GCC) $(GCC_OPT) $(INC) -MM $(CFLAGS) $< > $@

-include $(DEPS)

$(MTCP_HDR):
	cp $(INC_DIR)/$@ $(MTCP_HDR_DIR)/$@

clean: clean-library
	rm -f *.o *~ core
	rm -f .*.d

clean-library:
	rm -f $(MTCP_LIB_DIR)/*
	rm -f $(MTCP_HDR_DIR)/*

distclean: clean
	rm -f Makefile
